********************************************************************************
**  TITLE:      ca2011_ipsos_contact_networks                                 **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       2011 Canada General Election Exit Survey                      **
**	DATE:		October 2022 					                              **	
**  VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "ca2011_ipsos_contact_networks", replace

* Open the dataset

import spss using "2011 Canada General Election Exit Survey.sav", clear case(lower)


***********************
** DISTRICT OUTCOMES **
***********************

decode ridding, generate(riding_str)
gen str riding_code = substr(riding_str,1,5)
replace riding_code = "" if riding_str == "Not specified"
destring riding_code, gen(riding)

* Election outcome in each district (1 = CPC, 2 = LPC, 3 = NDP, 4 = BQ, 5 = GPC, 6 = other)

gen district_outcome = .

replace district_outcome = 2 if riding == 10001
replace district_outcome = 2 if riding == 10002
replace district_outcome = 2 if riding == 10003
replace district_outcome = 1 if riding == 10004
replace district_outcome = 2 if riding == 10005
replace district_outcome = 3 if riding == 10006
replace district_outcome = 3 if riding == 10007
replace district_outcome = 2 if riding == 11001
replace district_outcome = 2 if riding == 11002
replace district_outcome = 1 if riding == 11003
replace district_outcome = 2 if riding == 11004
replace district_outcome = 2 if riding == 12001
replace district_outcome = 1 if riding == 12002
replace district_outcome = 1 if riding == 12007
replace district_outcome = 3 if riding == 12003
replace district_outcome = 3 if riding == 12004
replace district_outcome = 2 if riding == 12005
replace district_outcome = 2 if riding == 12006
replace district_outcome = 3 if riding == 12008
replace district_outcome = 1 if riding == 12009
replace district_outcome = 2 if riding == 12010
replace district_outcome = 1 if riding == 12011
replace district_outcome = 3 if riding == 13001
replace district_outcome = 2 if riding == 13002
replace district_outcome = 1 if riding == 13003
replace district_outcome = 1 if riding == 13004
replace district_outcome = 1 if riding == 13005
replace district_outcome = 1 if riding == 13006
replace district_outcome = 1 if riding == 13007
replace district_outcome = 1 if riding == 13008
replace district_outcome = 1 if riding == 13009
replace district_outcome = 1 if riding == 13010
replace district_outcome = 3 if riding == 24046
replace district_outcome = 3 if riding == 24001
replace district_outcome = 4 if riding == 24002
replace district_outcome = 3 if riding == 24003
replace district_outcome = 3 if riding == 24004
replace district_outcome = 4 if riding == 24054
replace district_outcome = 1 if riding == 24005
replace district_outcome = 3 if riding == 24006
replace district_outcome = 3 if riding == 24007
replace district_outcome = 3 if riding == 24008
replace district_outcome = 2 if riding == 24009
replace district_outcome = 3 if riding == 24010
replace district_outcome = 3 if riding == 24011
replace district_outcome = 3 if riding == 24012
replace district_outcome = 3 if riding == 24013
replace district_outcome = 3 if riding == 24015
replace district_outcome = 3 if riding == 24016
replace district_outcome = 3 if riding == 24017
replace district_outcome = 3 if riding == 24018
replace district_outcome = 3 if riding == 24019
replace district_outcome = 3 if riding == 24020
replace district_outcome = 4 if riding == 24041
replace district_outcome = 3 if riding == 24021
replace district_outcome = 3 if riding == 24022
replace district_outcome = 3 if riding == 24023
replace district_outcome = 3 if riding == 24024
replace district_outcome = 3 if riding == 24025
replace district_outcome = 3 if riding == 24026
replace district_outcome = 2 if riding == 24027
replace district_outcome = 3 if riding == 24028
replace district_outcome = 3 if riding == 24029
replace district_outcome = 3 if riding == 24030
replace district_outcome = 3 if riding == 24031
replace district_outcome = 3 if riding == 24032
replace district_outcome = 3 if riding == 24033
replace district_outcome = 1 if riding == 24034
replace district_outcome = 3 if riding == 24035
replace district_outcome = 1 if riding == 24036
replace district_outcome = 3 if riding == 24037
replace district_outcome = 3 if riding == 24038
replace district_outcome = 3 if riding == 24039
replace district_outcome = 3 if riding == 24040
replace district_outcome = 1 if riding == 24042
replace district_outcome = 3 if riding == 24043
replace district_outcome = 3 if riding == 24058
replace district_outcome = 3 if riding == 24014
replace district_outcome = 2 if riding == 24044
replace district_outcome = 3 if riding == 24045
replace district_outcome = 3 if riding == 24047
replace district_outcome = 2 if riding == 24048
replace district_outcome = 3 if riding == 24049
replace district_outcome = 3 if riding == 24050
replace district_outcome = 3 if riding == 24051
replace district_outcome = 3 if riding == 24052
replace district_outcome = 3 if riding == 24053
replace district_outcome = 4 if riding == 24055
replace district_outcome = 3 if riding == 24056
replace district_outcome = 3 if riding == 24057
replace district_outcome = 3 if riding == 24059
replace district_outcome = 1 if riding == 24060
replace district_outcome = 3 if riding == 24061
replace district_outcome = 3 if riding == 24062
replace district_outcome = 3 if riding == 24063
replace district_outcome = 3 if riding == 24064
replace district_outcome = 3 if riding == 24065
replace district_outcome = 2 if riding == 24066
replace district_outcome = 2 if riding == 24067
replace district_outcome = 3 if riding == 24068
replace district_outcome = 3 if riding == 24069
replace district_outcome = 3 if riding == 24070
replace district_outcome = 3 if riding == 24071
replace district_outcome = 3 if riding == 24072
replace district_outcome = 3 if riding == 24073
replace district_outcome = 3 if riding == 24074
replace district_outcome = 2 if riding == 24075
replace district_outcome = 1 if riding == 35001
replace district_outcome = 3 if riding == 35002
replace district_outcome = 1 if riding == 35003
replace district_outcome = 1 if riding == 35004
replace district_outcome = 3 if riding == 35005
replace district_outcome = 1 if riding == 35006
replace district_outcome = 1 if riding == 35007
replace district_outcome = 1 if riding == 35008
replace district_outcome = 1 if riding == 35009
replace district_outcome = 1 if riding == 35026
replace district_outcome = 1 if riding == 35010
replace district_outcome = 1 if riding == 35011
replace district_outcome = 1 if riding == 35012
replace district_outcome = 1 if riding == 35013
replace district_outcome = 3 if riding == 35015
replace district_outcome = 1 if riding == 35016
replace district_outcome = 1 if riding == 35017
replace district_outcome = 1 if riding == 35018
replace district_outcome = 1 if riding == 35014
replace district_outcome = 1 if riding == 35019
replace district_outcome = 1 if riding == 35020
replace district_outcome = 1 if riding == 35021
replace district_outcome = 1 if riding == 35022
replace district_outcome = 1 if riding == 35023
replace district_outcome = 2 if riding == 35024
replace district_outcome = 1 if riding == 35025
replace district_outcome = 2 if riding == 35027
replace district_outcome = 1 if riding == 35028
replace district_outcome = 1 if riding == 35029
replace district_outcome = 1 if riding == 35030
replace district_outcome = 3 if riding == 35031
replace district_outcome = 3 if riding == 35032
replace district_outcome = 3 if riding == 35033
replace district_outcome = 1 if riding == 35034
replace district_outcome = 1 if riding == 35035
replace district_outcome = 2 if riding == 35036
replace district_outcome = 1 if riding == 35037
replace district_outcome = 1 if riding == 35038
replace district_outcome = 1 if riding == 35039
replace district_outcome = 1 if riding == 35046
replace district_outcome = 1 if riding == 35040
replace district_outcome = 1 if riding == 35041
replace district_outcome = 3 if riding == 35042
replace district_outcome = 1 if riding == 35043
replace district_outcome = 1 if riding == 35044
replace district_outcome = 2 if riding == 35045
replace district_outcome = 1 if riding == 35047
replace district_outcome = 1 if riding == 35048
replace district_outcome = 1 if riding == 35049
replace district_outcome = 1 if riding == 35050
replace district_outcome = 1 if riding == 35051
replace district_outcome = 1 if riding == 35052
replace district_outcome = 1 if riding == 35053
replace district_outcome = 1 if riding == 35054
replace district_outcome = 1 if riding == 35055
replace district_outcome = 3 if riding == 35056
replace district_outcome = 1 if riding == 35057
replace district_outcome = 1 if riding == 35058
replace district_outcome = 1 if riding == 35059
replace district_outcome = 1 if riding == 35060
replace district_outcome = 1 if riding == 35061
replace district_outcome = 3 if riding == 35062
replace district_outcome = 1 if riding == 35063
replace district_outcome = 2 if riding == 35064
replace district_outcome = 2 if riding == 35065
replace district_outcome = 1 if riding == 35066
replace district_outcome = 1 if riding == 35067
replace district_outcome = 3 if riding == 35068
replace district_outcome = 1 if riding == 35069
replace district_outcome = 1 if riding == 35070
replace district_outcome = 1 if riding == 35071
replace district_outcome = 1 if riding == 35072
replace district_outcome = 1 if riding == 35073
replace district_outcome = 1 if riding == 35074
replace district_outcome = 1 if riding == 35075
replace district_outcome = 1 if riding == 35076
replace district_outcome = 2 if riding == 35077
replace district_outcome = 1 if riding == 35078
replace district_outcome = 1 if riding == 35079
replace district_outcome = 2 if riding == 35080
replace district_outcome = 1 if riding == 35081
replace district_outcome = 2 if riding == 35082
replace district_outcome = 3 if riding == 35083
replace district_outcome = 3 if riding == 35084
replace district_outcome = 1 if riding == 35085
replace district_outcome = 1 if riding == 35086
replace district_outcome = 1 if riding == 35087
replace district_outcome = 3 if riding == 35088
replace district_outcome = 1 if riding == 35089
replace district_outcome = 3 if riding == 35090
replace district_outcome = 3 if riding == 35091
replace district_outcome = 3 if riding == 35092
replace district_outcome = 2 if riding == 35093
replace district_outcome = 3 if riding == 35094
replace district_outcome = 3 if riding == 35095
replace district_outcome = 1 if riding == 35096
replace district_outcome = 3 if riding == 35097
replace district_outcome = 1 if riding == 35098
replace district_outcome = 1 if riding == 35099
replace district_outcome = 1 if riding == 35100
replace district_outcome = 3 if riding == 35101
replace district_outcome = 3 if riding == 35102
replace district_outcome = 1 if riding == 35103
replace district_outcome = 1 if riding == 35104
replace district_outcome = 3 if riding == 35105
replace district_outcome = 2 if riding == 35106
replace district_outcome = 1 if riding == 46001
replace district_outcome = 1 if riding == 46002
replace district_outcome = 3 if riding == 46003
replace district_outcome = 1 if riding == 46004
replace district_outcome = 1 if riding == 46005
replace district_outcome = 1 if riding == 46006
replace district_outcome = 1 if riding == 46007
replace district_outcome = 1 if riding == 46008
replace district_outcome = 1 if riding == 46009
replace district_outcome = 1 if riding == 46010
replace district_outcome = 3 if riding == 46011
replace district_outcome = 2 if riding == 46012
replace district_outcome = 1 if riding == 46013
replace district_outcome = 1 if riding == 46014
replace district_outcome = 1 if riding == 47001
replace district_outcome = 1 if riding == 47002
replace district_outcome = 1 if riding == 47004
replace district_outcome = 1 if riding == 47003
replace district_outcome = 1 if riding == 47005
replace district_outcome = 1 if riding == 47006
replace district_outcome = 1 if riding == 47007
replace district_outcome = 1 if riding == 47008
replace district_outcome = 1 if riding == 47009
replace district_outcome = 1 if riding == 47010
replace district_outcome = 1 if riding == 47011
replace district_outcome = 1 if riding == 47012
replace district_outcome = 2 if riding == 47013
replace district_outcome = 1 if riding == 47014
replace district_outcome = 1 if riding == 48006
replace district_outcome = 1 if riding == 48003
replace district_outcome = 1 if riding == 48002
replace district_outcome = 1 if riding == 48004
replace district_outcome = 1 if riding == 48005
replace district_outcome = 1 if riding == 48007
replace district_outcome = 1 if riding == 48008
replace district_outcome = 1 if riding == 48009
replace district_outcome = 1 if riding == 48010
replace district_outcome = 1 if riding == 48012
replace district_outcome = 1 if riding == 48013
replace district_outcome = 1 if riding == 48014
replace district_outcome = 1 if riding == 48011
replace district_outcome = 1 if riding == 48015
replace district_outcome = 1 if riding == 48016
replace district_outcome = 1 if riding == 48017
replace district_outcome = 3 if riding == 48018
replace district_outcome = 1 if riding == 48001
replace district_outcome = 1 if riding == 48019
replace district_outcome = 1 if riding == 48020
replace district_outcome = 1 if riding == 48021
replace district_outcome = 1 if riding == 48022
replace district_outcome = 1 if riding == 48023
replace district_outcome = 1 if riding == 48024
replace district_outcome = 1 if riding == 48025
replace district_outcome = 1 if riding == 48026
replace district_outcome = 1 if riding == 48027
replace district_outcome = 1 if riding == 48028
replace district_outcome = 1 if riding == 59001
replace district_outcome = 3 if riding == 59026
replace district_outcome = 3 if riding == 59002
replace district_outcome = 3 if riding == 59003
replace district_outcome = 1 if riding == 59004
replace district_outcome = 1 if riding == 59005
replace district_outcome = 1 if riding == 59006
replace district_outcome = 3 if riding == 59008
replace district_outcome = 1 if riding == 59009
replace district_outcome = 1 if riding == 59010
replace district_outcome = 1 if riding == 59011
replace district_outcome = 1 if riding == 59012
replace district_outcome = 1 if riding == 59013
replace district_outcome = 1 if riding == 59014
replace district_outcome = 3 if riding == 59015
replace district_outcome = 3 if riding == 59016
replace district_outcome = 3 if riding == 59017
replace district_outcome = 1 if riding == 59019
replace district_outcome = 1 if riding == 59020
replace district_outcome = 1 if riding == 59018
replace district_outcome = 1 if riding == 59007
replace district_outcome = 1 if riding == 59021
replace district_outcome = 1 if riding == 59022
replace district_outcome = 1 if riding == 59023
replace district_outcome = 5 if riding == 59024
replace district_outcome = 3 if riding == 59025
replace district_outcome = 1 if riding == 59027
replace district_outcome = 3 if riding == 59028
replace district_outcome = 2 if riding == 59029
replace district_outcome = 3 if riding == 59030
replace district_outcome = 1 if riding == 59031
replace district_outcome = 3 if riding == 59032
replace district_outcome = 2 if riding == 59033
replace district_outcome = 1 if riding == 59034
replace district_outcome = 3 if riding == 59035
replace district_outcome = 1 if riding == 59036
replace district_outcome = 1 if riding == 60001
replace district_outcome = 3 if riding == 61001
replace district_outcome = 1 if riding == 62001


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 3.5 if riding == 10001
replace margin = 30.1 if riding == 10002
replace margin = 31.8 if riding == 10003
replace margin = .7000008 if riding == 10004
replace margin = 17.7 if riding == 10005
replace margin = 50.3 if riding == 10006
replace margin = 19.4 if riding == 10007
replace margin = 11.2 if riding == 11001
replace margin = 6.799999 if riding == 11002
replace margin = 23.3 if riding == 11003
replace margin = 3.300003 if riding == 11004
replace margin = 15.8 if riding == 12001
replace margin = 32 if riding == 12002
replace margin = 1.200001 if riding == 12003
replace margin = 26 if riding == 12004
replace margin = 5.400002 if riding == 12005
replace margin = 3 if riding == 12006
replace margin = 29.3 if riding == 12007
replace margin = 23.6 if riding == 12008
replace margin = 7 if riding == 12009
replace margin = 2.100002 if riding == 12010
replace margin = 10.6 if riding == 12011
replace margin = 53.5 if riding == 13001
replace margin = 5.799999 if riding == 13002
replace margin = 24.6 if riding == 13003
replace margin = 31.1 if riding == 13004
replace margin = 5.399998 if riding == 13005
replace margin = 29.3 if riding == 13006
replace margin = 4.400002 if riding == 13007
replace margin = 33.4 if riding == 13008
replace margin = 19 if riding == 13009
replace margin = 43.7 if riding == 13010
replace margin = 19.6 if riding == 24001
replace margin = 1.5 if riding == 24002
replace margin = 19.3 if riding == 24003
replace margin = 15.3 if riding == 24004
replace margin = 20.7 if riding == 24005
replace margin = 10.6 if riding == 24006
replace margin = 19.9 if riding == 24007
replace margin = 10.2 if riding == 24008
replace margin = 8.600002 if riding == 24009
replace margin = 20.5 if riding == 24010
replace margin = 13.7 if riding == 24011
replace margin = 15 if riding == 24012
replace margin = 14.7 if riding == 24013
replace margin = 2.399998 if riding == 24014
replace margin = 25.3 if riding == 24015
replace margin = 9.299999 if riding == 24016
replace margin = 21.6 if riding == 24017
replace margin = 29.6 if riding == 24018
replace margin = 2.199999 if riding == 24019
replace margin = 46.7 if riding == 24020
replace margin = 17 if riding == 24021
replace margin = 6.000002 if riding == 24022
replace margin = 38.9 if riding == 24023
replace margin = 20.5 if riding == 24024
replace margin = 14.4 if riding == 24025
replace margin = 8.200001 if riding == 24026
replace margin = 4.099998 if riding == 24027
replace margin = 15.8 if riding == 24028
replace margin = 15.5 if riding == 24029
replace margin = 12.3 if riding == 24030
replace margin = 10.7 if riding == 24031
replace margin = 20.6 if riding == 24032
replace margin = 27 if riding == 24033
replace margin = 10.1 if riding == 24034
replace margin = 24.7 if riding == 24035
replace margin = 1.400002 if riding == 24036
replace margin = 14.5 if riding == 24037
replace margin = 2.300003 if riding == 24038
replace margin = 17.7 if riding == 24039
replace margin = 23.3 if riding == 24040
replace margin = 10.5 if riding == 24041
replace margin = 22.9 if riding == 24042
replace margin = 22.8 if riding == 24043
replace margin = 5.800003 if riding == 24044
replace margin = 7.800001 if riding == 24045
replace margin = 22.1 if riding == 24046
replace margin = 32.7 if riding == 24047
replace margin = 10.1 if riding == 24048
replace margin = 3.599998 if riding == 24049
replace margin = 16.2 if riding == 24050
replace margin = 14.9 if riding == 24051
replace margin = 14.6 if riding == 24052
replace margin = 20.8 if riding == 24053
replace margin = 2.700001 if riding == 24054
replace margin = 1.299999 if riding == 24055
replace margin = 12.2 if riding == 24056
replace margin = 20.7 if riding == 24057
replace margin = .1000023 if riding == 24058
replace margin = 27.1 if riding == 24059
replace margin = 18 if riding == 24060
replace margin = 18.2 if riding == 24061
replace margin = 16.4 if riding == 24062
replace margin = 27.8 if riding == 24063
replace margin = 17 if riding == 24064
replace margin = 16.7 if riding == 24065
replace margin = 14.1 if riding == 24066
replace margin = 10 if riding == 24067
replace margin = 9.799999 if riding == 24068
replace margin = 27.7 if riding == 24069
replace margin = 7.199997 if riding == 24070
replace margin = 18.5 if riding == 24071
replace margin = 29.8 if riding == 24072
replace margin = 17.9 if riding == 24073
replace margin = 6.899998 if riding == 24074
replace margin = 1.600002 if riding == 24075
replace margin = 5.799999 if riding == 35001
replace margin = 21.4 if riding == 35002
replace margin = 26.6 if riding == 35003
replace margin = 35.8 if riding == 35004
replace margin = 10.8 if riding == 35005
replace margin = .9000015 if riding == 35006
replace margin = 20.4 if riding == 35007
replace margin = 9.799999 if riding == 35008
replace margin = 20.4 if riding == 35009
replace margin = 30.9 if riding == 35010
replace margin = 25.7 if riding == 35011
replace margin = 33 if riding == 35012
replace margin = 27.5 if riding == 35013
replace margin = 33.4 if riding == 35014
replace margin = 25.8 if riding == 35015
replace margin = 2.200001 if riding == 35016
replace margin = 1.100002 if riding == 35017
replace margin = 44.3 if riding == 35018
replace margin = 8.399998 if riding == 35019
replace margin = 32.9 if riding == 35020
replace margin = 12.9 if riding == 35021
replace margin = 0 if riding == 35022
replace margin = 5.200001 if riding == 35023
replace margin = 10.3 if riding == 35024
replace margin = 18.1 if riding == 35025
replace margin = 38.7 if riding == 35026
replace margin = 10.6 if riding == 35027
replace margin = 26 if riding == 35028
replace margin = 37.9 if riding == 35029
replace margin = 28.7 if riding == 35030
replace margin = 30.6 if riding == 35031
replace margin = 9 if riding == 35032
replace margin = 14.1 if riding == 35033
replace margin = 29.6 if riding == 35034
replace margin = 19.1 if riding == 35035
replace margin = 4.399998 if riding == 35036
replace margin = 11.1 if riding == 35037
replace margin = 32.3 if riding == 35038
replace margin = 3.300003 if riding == 35039
replace margin = 36.6 if riding == 35040
replace margin = 42.5 if riding == 35041
replace margin = 17.4 if riding == 35042
replace margin = 3.200001 if riding == 35043
replace margin = 17.7 if riding == 35044
replace margin = 3.400002 if riding == 35045
replace margin = 33.7 if riding == 35046
replace margin = 9.5 if riding == 35047
replace margin = 1.5 if riding == 35048
replace margin = 13.1 if riding == 35049
replace margin = 9.299999 if riding == 35050
replace margin = 6.900002 if riding == 35051
replace margin = 29.3 if riding == 35052
replace margin = 30.4 if riding == 35053
replace margin = 29.8 if riding == 35054
replace margin = 35.7 if riding == 35055
replace margin = 27 if riding == 35056
replace margin = .1000023 if riding == 35057
replace margin = 32.8 if riding == 35058
replace margin = 22.8 if riding == 35059
replace margin = 20.9 if riding == 35060
replace margin = 13.4 if riding == 35061
replace margin = 30.4 if riding == 35062
replace margin = 6.199997 if riding == 35063
replace margin = 10.7 if riding == 35064
replace margin = 8.800001 if riding == 35065
replace margin = 13.2 if riding == 35066
replace margin = 33.3 if riding == 35067
replace margin = 14.3 if riding == 35068
replace margin = 31.5 if riding == 35069
replace margin = 33.2 if riding == 35070
replace margin = 24.8 if riding == 35071
replace margin = 2.5 if riding == 35072
replace margin = 29.6 if riding == 35073
replace margin = 34.7 if riding == 35074
replace margin = 8.799999 if riding == 35075
replace margin = 27.1 if riding == 35076
replace margin = 8.199997 if riding == 35077
replace margin = 22.7 if riding == 35078
replace margin = 4.200001 if riding == 35079
replace margin = 11.2 if riding == 35080
replace margin = 3.899998 if riding == 35081
replace margin = 1.799999 if riding == 35082
replace margin = 10.7 if riding == 35083
replace margin = 3.200001 if riding == 35084
replace margin = 32 if riding == 35085
replace margin = 34.6 if riding == 35086
replace margin = 44.2 if riding == 35087
replace margin = 21.6 if riding == 35088
replace margin = 37.7 if riding == 35089
replace margin = 21.5 if riding == 35090
replace margin = 20.2 if riding == 35091
replace margin = 18.7 if riding == 35092
replace margin = 10.8 if riding == 35093
replace margin = 43.2 if riding == 35094
replace margin = 31.1 if riding == 35095
replace margin = 26.4 if riding == 35096
replace margin = 2 if riding == 35097
replace margin = 47.3 if riding == 35098
replace margin = 36.1 if riding == 35099
replace margin = 1.799999 if riding == 35100
replace margin = 16.3 if riding == 35101
replace margin = 22.7 if riding == 35102
replace margin = 15.2 if riding == 35103
replace margin = 44.3 if riding == 35104
replace margin = 7.299999 if riding == 35105
replace margin = 19.2 if riding == 35106
replace margin = 38.5 if riding == 46001
replace margin = 37.5 if riding == 46002
replace margin = 24.9 if riding == 46003
replace margin = 37 if riding == 46004
replace margin = .9000015 if riding == 46005
replace margin = 28.1 if riding == 46006
replace margin = 66.2 if riding == 46007
replace margin = 52.7 if riding == 46008
replace margin = 19.5 if riding == 46009
replace margin = 38.7 if riding == 46010
replace margin = 26.1 if riding == 46011
replace margin = .2000008 if riding == 46012
replace margin = 19.5 if riding == 46013
replace margin = 1.799999 if riding == 46014
replace margin = 39.8 if riding == 47001
replace margin = 17.6 if riding == 47002
replace margin = 3.600002 if riding == 47003
replace margin = 48.6 if riding == 47004
replace margin = 2.299999 if riding == 47005
replace margin = 30.4 if riding == 47006
replace margin = 16.4 if riding == 47007
replace margin = 15.1 if riding == 47008
replace margin = 17.6 if riding == 47009
replace margin = 1.799999 if riding == 47010
replace margin = 27 if riding == 47011
replace margin = 55.3 if riding == 47012
replace margin = 3.899998 if riding == 47013
replace margin = 47.1 if riding == 47014
replace margin = 58.6 if riding == 48001
replace margin = 53.3 if riding == 48002
replace margin = 40.5 if riding == 48003
replace margin = 29.1 if riding == 48004
replace margin = 57.7 if riding == 48005
replace margin = 40.2 if riding == 48006
replace margin = 66 if riding == 48007
replace margin = 63.2 if riding == 48008
replace margin = 44.5 if riding == 48009
replace margin = 74.9 if riding == 48010
replace margin = 37.2 if riding == 48011
replace margin = 22.6 if riding == 48012
replace margin = 15.3 if riding == 48013
replace margin = 44.3 if riding == 48014
replace margin = 42.1 if riding == 48015
replace margin = 15.2 if riding == 48016
replace margin = 55.3 if riding == 48017
replace margin = 12.9 if riding == 48018
replace margin = 29.3 if riding == 48019
replace margin = 67.2 if riding == 48020
replace margin = 58.4 if riding == 48021
replace margin = 59.7 if riding == 48022
replace margin = 60.8 if riding == 48023
replace margin = 68.5 if riding == 48024
replace margin = 65.60001 if riding == 48025
replace margin = 70 if riding == 48026
replace margin = 63.4 if riding == 48027
replace margin = 63.9 if riding == 48028
replace margin = 44.8 if riding == 59001
replace margin = 2.099998 if riding == 59002
replace margin = 13.9 if riding == 59003
replace margin = 26 if riding == 59004
replace margin = 31.4 if riding == 59005
replace margin = 30.9 if riding == 59006
replace margin = 18.8 if riding == 59007
replace margin = .7000008 if riding == 59008
replace margin = 14.7 if riding == 59009
replace margin = 15.3 if riding == 59010
replace margin = 35.3 if riding == 59011
replace margin = 22.7 if riding == 59012
replace margin = 44 if riding == 59013
replace margin = 8.100002 if riding == 59014
replace margin = 10.6 if riding == 59015
replace margin = 1.900002 if riding == 59016
replace margin = 4.400002 if riding == 59017
replace margin = 29 if riding == 59018
replace margin = 19 if riding == 59019
replace margin = 29.5 if riding == 59020
replace margin = 26 if riding == 59021
replace margin = 36.5 if riding == 59022
replace margin = 39.7 if riding == 59023
replace margin = 10.6 if riding == 59024
replace margin = 20.8 if riding == 59025
replace margin = 12 if riding == 59026
replace margin = 34.3 if riding == 59027
replace margin = 4.100002 if riding == 59028
replace margin = 5 if riding == 59029
replace margin = 43.9 if riding == 59030
replace margin = 3.099998 if riding == 59031
replace margin = 22 if riding == 59032
replace margin = 3.600002 if riding == 59033
replace margin = 8.599998 if riding == 59034
replace margin = 27.2 if riding == 59035
replace margin = 21.9 if riding == 59036
replace margin = .8999977 if riding == 60001
replace margin = 13.7 if riding == 61001
replace margin = 21.2 if riding == 62001

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.64628 if riding == 10001
replace enep = 2.335194 if riding == 10002
replace enep = 2.41807 if riding == 10003
replace enep = 2.851782 if riding == 10004
replace enep = 2.637569 if riding == 10005
replace enep = 1.800158 if riding == 10006
replace enep = 2.757107 if riding == 10007
replace enep = 2.474023 if riding == 11001
replace enep = 3.062703 if riding == 11002
replace enep = 2.428658 if riding == 11003
replace enep = 2.812987 if riding == 11004
replace enep = 2.86726 if riding == 12001
replace enep = 2.454584 if riding == 12002
replace enep = 3.145465 if riding == 12003
replace enep = 2.731218 if riding == 12004
replace enep = 3.229933 if riding == 12005
replace enep = 3.01009 if riding == 12006
replace enep = 2.738976 if riding == 12007
replace enep = 2.499913 if riding == 12008
replace enep = 2.889447 if riding == 12009
replace enep = 2.948035 if riding == 12010
replace enep = 2.689748 if riding == 12011
replace enep = 1.879933 if riding == 13001
replace enep = 3.121342 if riding == 13002
replace enep = 2.887169 if riding == 13003
replace enep = 2.364256 if riding == 13004
replace enep = 3.069914 if riding == 13005
replace enep = 2.650031 if riding == 13006
replace enep = 3.225359 if riding == 13007
replace enep = 2.529692 if riding == 13008
replace enep = 2.717598 if riding == 13009
replace enep = 2.19715 if riding == 13010
replace enep = 2.663272 if riding == 24001
replace enep = 3.605175 if riding == 24002
replace enep = 3.443408 if riding == 24003
replace enep = 3.24391 if riding == 24004
replace enep = 2.748053 if riding == 24005
replace enep = 3.068219 if riding == 24006
replace enep = 3.098537 if riding == 24007
replace enep = 3.527262 if riding == 24008
replace enep = 3.272894 if riding == 24009
replace enep = 3.444855 if riding == 24010
replace enep = 3.456261 if riding == 24011
replace enep = 3.495806 if riding == 24012
replace enep = 3.073377 if riding == 24013
replace enep = 3.263921 if riding == 24014
replace enep = 2.772671 if riding == 24015
replace enep = 3.38382 if riding == 24016
replace enep = 3.092471 if riding == 24017
replace enep = 2.878245 if riding == 24018
replace enep = 3.752824 if riding == 24019
replace enep = 2.321872 if riding == 24020
replace enep = 2.887028 if riding == 24021
replace enep = 3.638997 if riding == 24022
replace enep = 2.442038 if riding == 24023
replace enep = 3.28513 if riding == 24024
replace enep = 2.883972 if riding == 24025
replace enep = 2.893811 if riding == 24026
replace enep = 3.444523 if riding == 24027
replace enep = 2.827383 if riding == 24028
replace enep = 3.479834 if riding == 24029
replace enep = 3.159747 if riding == 24030
replace enep = 2.794725 if riding == 24031
replace enep = 3.454124 if riding == 24032
replace enep = 3.209428 if riding == 24033
replace enep = 3.005214 if riding == 24034
replace enep = 2.769669 if riding == 24035
replace enep = 3.007365 if riding == 24036
replace enep = 3.648663 if riding == 24037
replace enep = 3.070848 if riding == 24038
replace enep = 2.828414 if riding == 24039
replace enep = 2.931262 if riding == 24040
replace enep = 3.779961 if riding == 24041
replace enep = 2.927624 if riding == 24042
replace enep = 2.61399 if riding == 24043
replace enep = 3.017948 if riding == 24044
replace enep = 3.445567 if riding == 24045
replace enep = 3.364975 if riding == 24046
replace enep = 2.569049 if riding == 24047
replace enep = 3.364455 if riding == 24048
replace enep = 3.503081 if riding == 24049
replace enep = 3.100237 if riding == 24050
replace enep = 3.261898 if riding == 24051
replace enep = 3.331557 if riding == 24052
replace enep = 2.645985 if riding == 24053
replace enep = 3.317564 if riding == 24054
replace enep = 3.493828 if riding == 24055
replace enep = 3.21858 if riding == 24056
replace enep = 2.9097 if riding == 24057
replace enep = 3.244067 if riding == 24058
replace enep = 2.521973 if riding == 24059
replace enep = 3.009166 if riding == 24060
replace enep = 2.644271 if riding == 24061
replace enep = 3.232229 if riding == 24062
replace enep = 2.754684 if riding == 24063
replace enep = 2.954489 if riding == 24064
replace enep = 3.375072 if riding == 24065
replace enep = 3.219409 if riding == 24066
replace enep = 3.2105 if riding == 24067
replace enep = 3.513457 if riding == 24068
replace enep = 2.890616 if riding == 24069
replace enep = 3.004627 if riding == 24070
replace enep = 2.825976 if riding == 24071
replace enep = 2.74207 if riding == 24072
replace enep = 3.364444 if riding == 24073
replace enep = 2.968328 if riding == 24074
replace enep = 3.331645 if riding == 24075
replace enep = 2.750547 if riding == 35001
replace enep = 2.618055 if riding == 35002
replace enep = 2.767906 if riding == 35003
replace enep = 2.535073 if riding == 35004
replace enep = 3.109704 if riding == 35005
replace enep = 3.203557 if riding == 35006
replace enep = 2.848954 if riding == 35007
replace enep = 2.817457 if riding == 35008
replace enep = 2.803264 if riding == 35009
replace enep = 2.599928 if riding == 35010
replace enep = 2.590734 if riding == 35011
replace enep = 2.464019 if riding == 35012
replace enep = 2.584019 if riding == 35013
replace enep = 2.655506 if riding == 35014
replace enep = 2.580279 if riding == 35015
replace enep = 3.130244 if riding == 35016
replace enep = 2.677469 if riding == 35017
replace enep = 2.473448 if riding == 35018
replace enep = 2.624948 if riding == 35019
replace enep = 2.438269 if riding == 35020
replace enep = 2.659532 if riding == 35021
replace enep = 2.764096 if riding == 35022
replace enep = 3.044808 if riding == 35023
replace enep = 2.949035 if riding == 35024
replace enep = 2.76868 if riding == 35025
replace enep = 2.605076 if riding == 35026
replace enep = 3.052727 if riding == 35027
replace enep = 2.760509 if riding == 35028
replace enep = 2.335996 if riding == 35029
replace enep = 2.561777 if riding == 35030
replace enep = 2.41037 if riding == 35031
replace enep = 2.826032 if riding == 35032
replace enep = 2.782361 if riding == 35033
replace enep = 2.542013 if riding == 35034
replace enep = 2.87841 if riding == 35035
replace enep = 3.084145 if riding == 35036
replace enep = 3.067249 if riding == 35037
replace enep = 2.621321 if riding == 35038
replace enep = 2.971 if riding == 35039
replace enep = 2.488237 if riding == 35040
replace enep = 2.320541 if riding == 35041
replace enep = 2.594141 if riding == 35042
replace enep = 3.186236 if riding == 35043
replace enep = 2.961559 if riding == 35044
replace enep = 3.068247 if riding == 35045
replace enep = 2.428092 if riding == 35046
replace enep = 2.807743 if riding == 35047
replace enep = 2.906318 if riding == 35048
replace enep = 2.753888 if riding == 35049
replace enep = 2.688389 if riding == 35050
replace enep = 2.817362 if riding == 35051
replace enep = 2.574307 if riding == 35052
replace enep = 2.64759 if riding == 35053
replace enep = 2.655471 if riding == 35054
replace enep = 2.505399 if riding == 35055
replace enep = 2.490251 if riding == 35056
replace enep = 3.169482 if riding == 35057
replace enep = 2.642581 if riding == 35058
replace enep = 2.700849 if riding == 35059
replace enep = 2.623329 if riding == 35060
replace enep = 2.422715 if riding == 35061
replace enep = 2.766481 if riding == 35062
replace enep = 2.722 if riding == 35063
replace enep = 2.953076 if riding == 35064
replace enep = 3.241397 if riding == 35065
replace enep = 2.946003 if riding == 35066
replace enep = 2.36079 if riding == 35067
replace enep = 2.805088 if riding == 35068
replace enep = 2.573234 if riding == 35069
replace enep = 2.65112 if riding == 35070
replace enep = 2.808082 if riding == 35071
replace enep = 2.957338 if riding == 35072
replace enep = 2.656141 if riding == 35073
replace enep = 2.820882 if riding == 35074
replace enep = 2.865165 if riding == 35075
replace enep = 2.780419 if riding == 35076
replace enep = 3.122443 if riding == 35077
replace enep = 2.587978 if riding == 35078
replace enep = 2.890574 if riding == 35079
replace enep = 2.806072 if riding == 35080
replace enep = 3.139698 if riding == 35081
replace enep = 3.123653 if riding == 35082
replace enep = 3.044048 if riding == 35083
replace enep = 3.23201 if riding == 35084
replace enep = 3.207781 if riding == 35085
replace enep = 2.65433 if riding == 35086
replace enep = 2.228164 if riding == 35087
replace enep = 2.759199 if riding == 35088
replace enep = 2.230863 if riding == 35089
replace enep = 2.786928 if riding == 35090
replace enep = 2.731882 if riding == 35091
replace enep = 2.634088 if riding == 35092
replace enep = 3.195715 if riding == 35093
replace enep = 2.35085 if riding == 35094
replace enep = 2.617842 if riding == 35095
replace enep = 2.379196 if riding == 35096
replace enep = 2.778094 if riding == 35097
replace enep = 2.204114 if riding == 35098
replace enep = 2.420862 if riding == 35099
replace enep = 2.725197 if riding == 35100
replace enep = 2.635303 if riding == 35101
replace enep = 2.454674 if riding == 35102
replace enep = 2.688721 if riding == 35103
replace enep = 2.191142 if riding == 35104
replace enep = 3.046756 if riding == 35105
replace enep = 2.878684 if riding == 35106
replace enep = 2.103323 if riding == 46001
replace enep = 2.453663 if riding == 46002
replace enep = 2.688823 if riding == 46003
replace enep = 2.116841 if riding == 46004
replace enep = 2.348619 if riding == 46005
replace enep = 2.289498 if riding == 46006
replace enep = 1.68113 if riding == 46007
replace enep = 1.865463 if riding == 46008
replace enep = 2.671554 if riding == 46009
replace enep = 2.004579 if riding == 46010
replace enep = 2.618192 if riding == 46011
replace enep = 3.077444 if riding == 46012
replace enep = 2.520714 if riding == 46013
replace enep = 3.045958 if riding == 46014
replace enep = 1.912704 if riding == 47001
replace enep = 2.294078 if riding == 47002
replace enep = 2.330622 if riding == 47003
replace enep = 1.863165 if riding == 47004
replace enep = 2.356168 if riding == 47005
replace enep = 2.041929 if riding == 47006
replace enep = 2.355679 if riding == 47007
replace enep = 2.289346 if riding == 47008
replace enep = 2.4496 if riding == 47009
replace enep = 2.182929 if riding == 47010
replace enep = 2.245692 if riding == 47011
replace enep = 1.708543 if riding == 47012
replace enep = 2.9201 if riding == 47013
replace enep = 1.895932 if riding == 47014
replace enep = 1.83221 if riding == 48001
replace enep = 2.034128 if riding == 48002
replace enep = 2.620531 if riding == 48003
replace enep = 2.42603 if riding == 48004
replace enep = 1.905495 if riding == 48005
replace enep = 2.528087 if riding == 48006
replace enep = 1.66355 if riding == 48007
replace enep = 1.706266 if riding == 48008
replace enep = 2.283726 if riding == 48009
replace enep = 1.397436 if riding == 48010
replace enep = 2.262275 if riding == 48011
replace enep = 2.88777 if riding == 48012
replace enep = 2.36291 if riding == 48013
replace enep = 2.178507 if riding == 48014
replace enep = 2.162602 if riding == 48015
replace enep = 3.177377 if riding == 48016
replace enep = 1.849872 if riding == 48017
replace enep = 2.210443 if riding == 48018
replace enep = 2.478333 if riding == 48019
replace enep = 1.62364 if riding == 48020
replace enep = 1.848497 if riding == 48021
replace enep = 1.65901 if riding == 48022
replace enep = 1.658576 if riding == 48023
replace enep = 1.530962 if riding == 48024
replace enep = 1.598951 if riding == 48025
replace enep = 1.474313 if riding == 48026
replace enep = 1.723615 if riding == 48027
replace enep = 1.629503 if riding == 48028
replace enep = 2.105307 if riding == 59001
replace enep = 2.731404 if riding == 59002
replace enep = 2.584547 if riding == 59003
replace enep = 2.417216 if riding == 59004
replace enep = 2.448166 if riding == 59005
replace enep = 2.638857 if riding == 59006
replace enep = 2.346988 if riding == 59007
replace enep = 2.891953 if riding == 59008
replace enep = 2.780102 if riding == 59009
replace enep = 2.414485 if riding == 59010
replace enep = 2.502659 if riding == 59011
replace enep = 2.337787 if riding == 59012
replace enep = 2.131396 if riding == 59013
replace enep = 2.685119 if riding == 59014
replace enep = 2.53735 if riding == 59015
replace enep = 3.227295 if riding == 59016
replace enep = 2.555453 if riding == 59017
replace enep = 2.540095 if riding == 59018
replace enep = 2.83874 if riding == 59019
replace enep = 2.729563 if riding == 59020
replace enep = 2.411655 if riding == 59021
replace enep = 2.185305 if riding == 59022
replace enep = 2.428817 if riding == 59023
replace enep = 2.780095 if riding == 59024
replace enep = 2.336361 if riding == 59025
replace enep = 2.404181 if riding == 59026
replace enep = 2.699121 if riding == 59027
replace enep = 3.127952 if riding == 59028
replace enep = 3.9204 if riding == 59029
replace enep = 2.243506 if riding == 59030
replace enep = 2.482918 if riding == 59031
replace enep = 2.784166 if riding == 59032
replace enep = 2.865001 if riding == 59033
replace enep = 2.901082 if riding == 59034
replace enep = 2.883373 if riding == 59035
replace enep = 3.140368 if riding == 59036
replace enep = 3.584975 if riding == 60001
replace enep = 2.87639 if riding == 61001
replace enep = 2.707048 if riding == 62001


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if riding == 10001
replace reelected = 1 if riding == 10002
replace reelected = 1 if riding == 10003
replace reelected = 0 if riding == 10004
replace reelected = 1 if riding == 10005
replace reelected = 1 if riding == 10006
replace reelected = 0 if riding == 10007
replace reelected = 1 if riding == 11001
replace reelected = 1 if riding == 11002
replace reelected = 1 if riding == 11003
replace reelected = 1 if riding == 11004
replace reelected = 1 if riding == 12001
replace reelected = 1 if riding == 12002
replace reelected = 0 if riding == 12003
replace reelected = 1 if riding == 12004
replace reelected = 1 if riding == 12005
replace reelected = 1 if riding == 12006
replace reelected = 0 if riding == 12007
replace reelected = 1 if riding == 12008
replace reelected = 1 if riding == 12009
replace reelected = 1 if riding == 12010
replace reelected = 1 if riding == 12011
replace reelected = 1 if riding == 13001
replace reelected = 1 if riding == 13002
replace reelected = 1 if riding == 13003
replace reelected = 1 if riding == 13004
replace reelected = 0 if riding == 13005
replace reelected = 1 if riding == 13006
replace reelected = 0 if riding == 13007
replace reelected = 1 if riding == 13008
replace reelected = 1 if riding == 13009
replace reelected = 1 if riding == 13010
replace reelected = 0 if riding == 24001
replace reelected = 1 if riding == 24002
replace reelected = 0 if riding == 24003
replace reelected = 0 if riding == 24004
replace reelected = 1 if riding == 24005
replace reelected = 0 if riding == 24006
replace reelected = 0 if riding == 24007
replace reelected = 0 if riding == 24008
replace reelected = 1 if riding == 24009
replace reelected = 0 if riding == 24010
replace reelected = 0 if riding == 24011
replace reelected = 0 if riding == 24012
replace reelected = 0 if riding == 24013
replace reelected = 0 if riding == 24014
replace reelected = 0 if riding == 24015
replace reelected = 0 if riding == 24016
replace reelected = 0 if riding == 24017
replace reelected = 0 if riding == 24018
replace reelected = 0 if riding == 24019
replace reelected = 0 if riding == 24020
replace reelected = 0 if riding == 24021
replace reelected = 0 if riding == 24022
replace reelected = 0 if riding == 24023
replace reelected = 0 if riding == 24024
replace reelected = 0 if riding == 24025
replace reelected = 0 if riding == 24026
replace reelected = 1 if riding == 24027
replace reelected = 0 if riding == 24028
replace reelected = 0 if riding == 24029
replace reelected = 0 if riding == 24030
replace reelected = 0 if riding == 24031
replace reelected = 0 if riding == 24032
replace reelected = 0 if riding == 24033
replace reelected = 1 if riding == 24034
replace reelected = 0 if riding == 24035
replace reelected = 1 if riding == 24036
replace reelected = 0 if riding == 24037
replace reelected = 0 if riding == 24038
replace reelected = 0 if riding == 24039
replace reelected = 0 if riding == 24040
replace reelected = 1 if riding == 24041
replace reelected = 1 if riding == 24042
replace reelected = 0 if riding == 24043
replace reelected = 1 if riding == 24044
replace reelected = 0 if riding == 24045
replace reelected = 0 if riding == 24046
replace reelected = 1 if riding == 24047
replace reelected = 1 if riding == 24048
replace reelected = 0 if riding == 24049
replace reelected = 0 if riding == 24050
replace reelected = 0 if riding == 24051
replace reelected = 0 if riding == 24052
replace reelected = 0 if riding == 24053
replace reelected = 1 if riding == 24054
replace reelected = 1 if riding == 24055
replace reelected = 0 if riding == 24056
replace reelected = 0 if riding == 24057
replace reelected = 0 if riding == 24058
replace reelected = 0 if riding == 24059
replace reelected = 1 if riding == 24060
replace reelected = 0 if riding == 24061
replace reelected = 0 if riding == 24062
replace reelected = 0 if riding == 24063
replace reelected = 0 if riding == 24064
replace reelected = 0 if riding == 24065
replace reelected = 1 if riding == 24066
replace reelected = 1 if riding == 24067
replace reelected = 0 if riding == 24068
replace reelected = 0 if riding == 24069
replace reelected = 0 if riding == 24070
replace reelected = 0 if riding == 24071
replace reelected = 0 if riding == 24072
replace reelected = 0 if riding == 24073
replace reelected = 0 if riding == 24074
replace reelected = 1 if riding == 24075
replace reelected = 0 if riding == 35001
replace reelected = 1 if riding == 35002
replace reelected = 1 if riding == 35003
replace reelected = 1 if riding == 35004
replace reelected = 0 if riding == 35005
replace reelected = 0 if riding == 35006
replace reelected = 0 if riding == 35007
replace reelected = 0 if riding == 35008
replace reelected = 1 if riding == 35009
replace reelected = 1 if riding == 35010
replace reelected = 1 if riding == 35011
replace reelected = 1 if riding == 35012
replace reelected = 1 if riding == 35013
replace reelected = 1 if riding == 35014
replace reelected = 0 if riding == 35015
replace reelected = 0 if riding == 35016
replace reelected = 0 if riding == 35017
replace reelected = 1 if riding == 35018
replace reelected = 0 if riding == 35019
replace reelected = 1 if riding == 35020
replace reelected = 1 if riding == 35021
replace reelected = 0 if riding == 35022
replace reelected = 0 if riding == 35023
replace reelected = 1 if riding == 35024
replace reelected = 1 if riding == 35025
replace reelected = 1 if riding == 35026
replace reelected = 1 if riding == 35027
replace reelected = 1 if riding == 35028
replace reelected = 1 if riding == 35029
replace reelected = 1 if riding == 35030
replace reelected = 1 if riding == 35031
replace reelected = 1 if riding == 35032
replace reelected = 1 if riding == 35033
replace reelected = 1 if riding == 35034
replace reelected = 1 if riding == 35035
replace reelected = 1 if riding == 35036
replace reelected = 1 if riding == 35037
replace reelected = 1 if riding == 35038
replace reelected = 1 if riding == 35039
replace reelected = 1 if riding == 35040
replace reelected = 1 if riding == 35041
replace reelected = 1 if riding == 35042
replace reelected = 0 if riding == 35043
replace reelected = 1 if riding == 35044
replace reelected = 1 if riding == 35045
replace reelected = 1 if riding == 35046
replace reelected = 0 if riding == 35047
replace reelected = 0 if riding == 35048
replace reelected = 1 if riding == 35049
replace reelected = 0 if riding == 35050
replace reelected = 0 if riding == 35051
replace reelected = 1 if riding == 35052
replace reelected = 1 if riding == 35053
replace reelected = 1 if riding == 35054
replace reelected = 1 if riding == 35055
replace reelected = 1 if riding == 35056
replace reelected = 0 if riding == 35057
replace reelected = 1 if riding == 35058
replace reelected = 1 if riding == 35059
replace reelected = 1 if riding == 35060
replace reelected = 1 if riding == 35061
replace reelected = 1 if riding == 35062
replace reelected = 1 if riding == 35063
replace reelected = 1 if riding == 35064
replace reelected = 1 if riding == 35065
replace reelected = 1 if riding == 35066
replace reelected = 1 if riding == 35067
replace reelected = 0 if riding == 35068
replace reelected = 1 if riding == 35069
replace reelected = 1 if riding == 35070
replace reelected = 1 if riding == 35071
replace reelected = 0 if riding == 35072
replace reelected = 1 if riding == 35073
replace reelected = 1 if riding == 35074
replace reelected = 0 if riding == 35075
replace reelected = 1 if riding == 35076
replace reelected = 1 if riding == 35077
replace reelected = 1 if riding == 35078
replace reelected = 0 if riding == 35079
replace reelected = 1 if riding == 35080
replace reelected = 0 if riding == 35081
replace reelected = 1 if riding == 35082
replace reelected = 0 if riding == 35083
replace reelected = 0 if riding == 35084
replace reelected = 1 if riding == 35085
replace reelected = 1 if riding == 35086
replace reelected = 1 if riding == 35087
replace reelected = 1 if riding == 35088
replace reelected = 1 if riding == 35089
replace reelected = 1 if riding == 35090
replace reelected = 1 if riding == 35091
replace reelected = 1 if riding == 35092
replace reelected = 1 if riding == 35093
replace reelected = 1 if riding == 35094
replace reelected = 1 if riding == 35095
replace reelected = 0 if riding == 35096
replace reelected = 1 if riding == 35097
replace reelected = 1 if riding == 35098
replace reelected = 1 if riding == 35099
replace reelected = 0 if riding == 35100
replace reelected = 1 if riding == 35101
replace reelected = 1 if riding == 35102
replace reelected = 0 if riding == 35103
replace reelected = 1 if riding == 35104
replace reelected = 0 if riding == 35105
replace reelected = 1 if riding == 35106
replace reelected = 1 if riding == 46001
replace reelected = 1 if riding == 46002
replace reelected = 1 if riding == 46003
replace reelected = 1 if riding == 46004
replace reelected = 0 if riding == 46005
replace reelected = 1 if riding == 46006
replace reelected = 1 if riding == 46007
replace reelected = 1 if riding == 46008
replace reelected = 1 if riding == 46009
replace reelected = 1 if riding == 46010
replace reelected = 1 if riding == 46011
replace reelected = 0 if riding == 46012
replace reelected = 1 if riding == 46013
replace reelected = 0 if riding == 46014
replace reelected = 1 if riding == 47001
replace reelected = 1 if riding == 47002
replace reelected = 1 if riding == 47003
replace reelected = 1 if riding == 47004
replace reelected = 1 if riding == 47005
replace reelected = 1 if riding == 47006
replace reelected = 1 if riding == 47007
replace reelected = 1 if riding == 47008
replace reelected = 1 if riding == 47009
replace reelected = 1 if riding == 47010
replace reelected = 1 if riding == 47011
replace reelected = 1 if riding == 47012
replace reelected = 1 if riding == 47013
replace reelected = 1 if riding == 47014
replace reelected = 1 if riding == 48001
replace reelected = 1 if riding == 48002
replace reelected = 1 if riding == 48003
replace reelected = 1 if riding == 48004
replace reelected = 1 if riding == 48005
replace reelected = 1 if riding == 48006
replace reelected = 1 if riding == 48007
replace reelected = 1 if riding == 48008
replace reelected = 1 if riding == 48009
replace reelected = 1 if riding == 48010
replace reelected = 1 if riding == 48011
replace reelected = 1 if riding == 48012
replace reelected = 1 if riding == 48013
replace reelected = 1 if riding == 48014
replace reelected = 1 if riding == 48015
replace reelected = 1 if riding == 48016
replace reelected = 1 if riding == 48017
replace reelected = 1 if riding == 48018
replace reelected = 1 if riding == 48019
replace reelected = 1 if riding == 48020
replace reelected = 1 if riding == 48021
replace reelected = 1 if riding == 48022
replace reelected = 1 if riding == 48023
replace reelected = 1 if riding == 48024
replace reelected = 1 if riding == 48025
replace reelected = 1 if riding == 48026
replace reelected = 1 if riding == 48027
replace reelected = 1 if riding == 48028
replace reelected = 1 if riding == 59001
replace reelected = 1 if riding == 59002
replace reelected = 1 if riding == 59003
replace reelected = 1 if riding == 59004
replace reelected = 1 if riding == 59005
replace reelected = 1 if riding == 59006
replace reelected = 1 if riding == 59007
replace reelected = 0 if riding == 59008
replace reelected = 1 if riding == 59009
replace reelected = 1 if riding == 59010
replace reelected = 1 if riding == 59011
replace reelected = 1 if riding == 59012
replace reelected = 1 if riding == 59013
replace reelected = 1 if riding == 59014
replace reelected = 1 if riding == 59015
replace reelected = 0 if riding == 59016
replace reelected = 1 if riding == 59017
replace reelected = 1 if riding == 59018
replace reelected = 1 if riding == 59019
replace reelected = 1 if riding == 59020
replace reelected = 1 if riding == 59021
replace reelected = 1 if riding == 59022
replace reelected = 1 if riding == 59023
replace reelected = 0 if riding == 59024
replace reelected = 1 if riding == 59025
replace reelected = 1 if riding == 59026
replace reelected = 1 if riding == 59027
replace reelected = 0 if riding == 59028
replace reelected = 1 if riding == 59029
replace reelected = 1 if riding == 59030
replace reelected = 1 if riding == 59031
replace reelected = 1 if riding == 59032
replace reelected = 1 if riding == 59033
replace reelected = 0 if riding == 59034
replace reelected = 1 if riding == 59035
replace reelected = 1 if riding == 59036
replace reelected = 0 if riding == 60001
replace reelected = 1 if riding == 61001
replace reelected = 1 if riding == 62001


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen time = 0

* Gender

gen male = gender

recode male (2=0)

* Age

gen r_age = age_val

replace r_age = . if r_age < 17

* Education

gen education = educ

* Vote status (national)

gen vote_whole = 1 if decided == 1
replace vote_whole = 0 if decided!=1 & decided!=. 

* Vote status (district)

gen vote_district = 1 if decided == district_outcome
replace vote_district = 0 if decided!=district_outcome & decided!=. 

* Political discussion

gen discussion = q28

recode discussion (5=.)

replace discussion = 4 - discussion

gen discussion_3pts = 0 if discussion == 0
replace discussion_3pts = 0 if discussion == 1
replace discussion_3pts = 1 if discussion == 2
replace discussion_3pts = 2 if discussion == 3


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecast_whole = q10a

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=1 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 1


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecasts_district = q10

* Identify correct forecasts

gen correct_district_d = 0 if forecasts_district!=district_outcome & forecasts_district!=.
replace correct_district_d = 1 if forecasts_district == district_outcome


**********
** SAVE **
**********

save "ca2011_ipsos.dta", replace


******************************
** MAIN ANALYSES (NATIONAL) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d discussion i.vote_whole r_age i.male education
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (NATIONAL) **
***********************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable vote_whole "National vote"
label variable vote_district "District vote"
label variable r_age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable enep "ENEP"
label variable margin "Margin"
label variable reelected "Reelected"

estpost tabstat correct_whole_d discussion vote_whole r_age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "ca2011_ipsos_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2011 Canadian federal election (Ipsos), national level") replace

eststo clear


******************************
** MAIN ANALYSES (DISTRICT) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (district)

melogit correct_district_d discussion i.vote_district r_age i.male education enep i.reelected || riding: , or
estimates table, star(.05 .01 .001)

tab correct_district_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (DISTRICT) **
***********************************

label variable correct_whole_d "Election winner (DV)"
label variable correct_district_d "District winner (DV)"
label variable discussion "Discussion"
label variable vote_whole "National vote"
label variable vote_district "District vote"
label variable r_age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable enep "ENEP"
label variable margin "Margin"
label variable reelected "Reelected"

estpost tabstat correct_district_d discussion vote_district r_age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "ca2011_ipsos_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2011 Canadian federal election (Ipsos), district level") replace

eststo clear

log close